home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / AlteredStates / Source / patch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-26  |  12.3 KB  |  444 lines  |  [TEXT/CWIE]

  1. #include<A4Stuff.h>
  2. #include<MixedMode.h>
  3. #include<Gestalt.h>
  4.  
  5. #include<stdio.h>
  6. #include<stdarg.h>
  7.  
  8. #define KeyDown(key) (( (keys)[key>>3] >> (key & 7) ) & 1)
  9.  
  10. enum{
  11.     stdBitsProcInfo = (    kPascalStackBased |
  12.                             STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(PixMap *))) |
  13.                             STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(Rect *))) |
  14.                             STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(Rect *))) |
  15.                             STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(short))) |
  16.                             STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(RgnHandle)))
  17.                         )
  18. };
  19.  
  20. void main(void);
  21. pascal OSErr GestaltSelector(OSType selector, long *response);
  22. pascal void NewStdBits(PixMap *srcMap, Rect *srcRect, Rect *destRect, short tMode, RgnHandle maskRgn);
  23.  
  24. void Invert(Rect *);
  25. void Dim(Rect *);
  26. void Blur(Rect *);
  27. void Apple2(Rect *);
  28. void Flip(Rect *);
  29. void NoBlue(Rect *);
  30. void Trace(Rect *);
  31. void Right(Rect *src);
  32. void Left(Rect *src);
  33.  
  34. void dprintf(char *format, ...);
  35.  
  36. RoutineDescriptor MainRD = BUILD_ROUTINE_DESCRIPTOR(kCStackBased, main);
  37. GWorldPtr gBraveNewGWorld;
  38. RoutineDescriptor *gStdBitsAddr;
  39. long gFiltersToUse = 0;
  40.  
  41. void main(void)
  42. {
  43. //    SelectorFunctionProcPtr gestaltProc;
  44.     UniversalProcPtr theProcPtr;
  45.     THz theZone;
  46.     OSErr err;
  47.     PixMapHandle    myPMap;
  48.     
  49.     InitGraf(&qd.thePort);
  50.  
  51.     theZone = GetZone();
  52.     SetZone(SystemZone());
  53.  
  54.     err = NewGWorld(&gBraveNewGWorld, 32, &qd.screenBits.bounds, nil, nil, 0);
  55.     
  56.     if ((err) || (gBraveNewGWorld == nil))
  57.     {
  58.         dprintf("Couldn't allocate GWorld, error %hd", (short)err);
  59.         SetZone(theZone);
  60.         return;
  61.     }
  62.     myPMap = GetGWorldPixMap(gBraveNewGWorld);
  63.     LockPixels(myPMap);
  64.  
  65.     DetachResource( GetResource('INIT',23) );
  66.     
  67.     gStdBitsAddr = NGetTrapAddress(_StdBits, ToolTrap);
  68.  
  69.     theProcPtr = NewRoutineDescriptor( (ProcPtr)NewStdBits, 
  70.                             stdBitsProcInfo,
  71.                             GetCurrentArchitecture() );
  72.  
  73.     NSetTrapAddress(theProcPtr, _StdBits, ToolTrap);
  74.  
  75.     NewSelectorFunctionProc(GestaltSelector);
  76.  
  77.     err = NewGestalt('AltS', NewSelectorFunctionProc(GestaltSelector));
  78.     if (err) {
  79.         dprintf("Couldn't newgestalt, error %hd", (short)err);
  80.         SetZone(theZone);
  81.         return;
  82.     }
  83.  
  84.     SetZone(theZone);
  85. }
  86.  
  87. pascal void NewStdBits(PixMap *srcMap, Rect *srcRect, Rect *destRect, short tMode, RgnHandle maskRgn)
  88. {
  89.     THz theZone;
  90.     Rect foozRect;
  91.     GWorldPtr oldGraf;
  92.     GDHandle oldGD;
  93.     int i;
  94.  
  95.     theZone = GetZone();
  96.     SetZone(SystemZone());
  97.  
  98.     foozRect = *srcRect;
  99.     OffsetRect(&foozRect, -foozRect.left, -foozRect.top);
  100.     
  101.     if(foozRect.right < 1024 && foozRect.bottom < 768 && gFiltersToUse) {
  102.         GetGWorld(&oldGraf, &oldGD);
  103.         SetGWorld(gBraveNewGWorld, nil);
  104.         
  105.         CALL_FIVE_PARAMETER_UPP( gStdBitsAddr, stdBitsProcInfo, srcMap, srcRect, &foozRect, srcCopy, nil);
  106.  
  107.         for(i=0;i<32;i++)
  108.         {
  109.             if(gFiltersToUse & 1 << i)
  110.             {
  111.                 switch(i)
  112.                 {
  113.                     case 0:
  114.                         Dim(&foozRect);
  115.                         break;
  116.                     case 1:
  117.                         Trace(&foozRect);
  118.                         break;
  119.                     case 2:
  120.                         Flip(&foozRect);
  121.                         break;
  122.                     case 3:
  123.                         Invert(&foozRect);
  124.                         break;
  125.                     case 4:
  126.                         Blur(&foozRect);
  127.                         break;
  128.                     case 5:
  129.                         Apple2(&foozRect);
  130.                         break;
  131.                     default:
  132.                         break;
  133.                 }
  134.             }
  135.         }
  136.  
  137.         SetGWorld(oldGraf, oldGD);
  138.  
  139.         CALL_FIVE_PARAMETER_UPP( gStdBitsAddr, stdBitsProcInfo, *GetGWorldPixMap(gBraveNewGWorld), &foozRect, destRect, tMode, maskRgn);
  140.         
  141.     } else {
  142.         CALL_FIVE_PARAMETER_UPP( gStdBitsAddr, stdBitsProcInfo, srcMap, srcRect, destRect, tMode, maskRgn);
  143.     }
  144.  
  145.     SetZone(theZone);
  146. }
  147.  
  148. pascal OSErr GestaltSelector(OSType, long *response) {
  149.     *response = (long) &gFiltersToUse;
  150.     return noErr;
  151. }
  152.  
  153. void Invert(Rect *foo) {
  154.     InvertRect(foo);
  155. }
  156.  
  157. void Dim(Rect *baz) {
  158.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  159.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  160.     Rect foo = bar[0]->bounds;
  161.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  162.     long x, y;
  163.     unsigned long *pixelptr;
  164.  
  165.     if(!data) {
  166.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  167.         return;
  168.     }
  169.  
  170.     for(y = baz->top; y < baz->bottom; y++) {
  171.         for(x = baz->left; x < baz->right; x++) {
  172.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  173.             *pixelptr -= (*pixelptr & 0xF0F0F0F0) >> 4;
  174. //            *(unsigned long*)(data + (y * rbytes) + (x * 4)) >>= 1;
  175.         }
  176.     }
  177. }
  178.  
  179. void Blur(Rect *baz) {
  180.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  181.     unsigned long rbytes = (bar[0]->rowBytes & 0x3FFF) >> 2;
  182.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  183.     long x, y;
  184.     unsigned long *pixelptr;
  185.  
  186.     if(!data) {
  187.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  188.         return;
  189.     }
  190.  
  191.     pixelptr = (unsigned long*)(data + (baz->top * (rbytes<<2)) + (baz->left * 4));
  192.     *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  193.         ( ( (*(pixelptr + 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  194.         ( ( (*(pixelptr + rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  195.  
  196.     pixelptr = (unsigned long*)(data + ((baz->bottom-1) * (rbytes<<2)) + (baz->left * 4));
  197.     *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  198.         ( ( (*(pixelptr + 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  199.         ( ( (*(pixelptr - rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  200.  
  201.     pixelptr = (unsigned long*)(data + (baz->top * (rbytes<<2)) + ((baz->right-1) * 4));
  202.     *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  203.         ( ( (*(pixelptr - 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  204.         ( ( (*(pixelptr + rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  205.  
  206.     pixelptr = (unsigned long*)(data + ((baz->bottom-1) * (rbytes<<2)) + ((baz->right-1) * 4));
  207.     *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  208.         ( ( (*(pixelptr - 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  209.         ( ( (*(pixelptr - rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  210.  
  211.     for(x = baz->left + 1; x < baz->right - 1; x++) {
  212.         pixelptr = (unsigned long*)(data + (baz->top * (rbytes<<2)) + (x * 4));
  213.         *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  214.             ( ( (*(pixelptr - 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  215.             ( ( (*(pixelptr + 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  216.             ( ( (*(pixelptr + rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  217.  
  218.         pixelptr = (unsigned long*)(data + ((baz->bottom-1) * (rbytes<<2)) + (x * 4));
  219.         *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  220.             ( ( (*(pixelptr - 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  221.             ( ( (*(pixelptr + 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  222.             ( ( (*(pixelptr - rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  223.     }
  224.  
  225.     for(y = baz->top + 1; y < baz->bottom - 1; y++) {
  226.         pixelptr = (unsigned long*)(data + (y * (rbytes<<2)) + (baz->left * 4));
  227.         *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  228.             ( ( (*(pixelptr + 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  229.             ( ( (*(pixelptr - rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  230.             ( ( (*(pixelptr + rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3);
  231.  
  232.         pixelptr = (unsigned long*)(data + (y * (rbytes<<2)) + ((baz->right-1) * 4));
  233.         *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  234.             ( ( (*(pixelptr - 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  235.             ( ( (*(pixelptr - rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  236.             ( ( (*(pixelptr + rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3);
  237.     }
  238.  
  239.     for(y = baz->top + 1; y < baz->bottom - 1; y++) {
  240.         for(x = baz->left + 1; x < baz->right - 1; x++) {
  241.             pixelptr = (unsigned long*)(data + (y * (rbytes<<2)) + (x * 4));
  242.             *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  243.                 ( ( (*(pixelptr - 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  244.                 ( ( (*(pixelptr + 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  245.                 ( ( (*(pixelptr - rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  246.                 ( ( (*(pixelptr + rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3);
  247.         }
  248.     }
  249. }
  250.  
  251. void Apple2(Rect *baz) {
  252.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  253.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  254.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  255.     long x, y;
  256.     unsigned long *pixelptr;
  257.     unsigned short quux;
  258.  
  259.     if(!data) {
  260.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  261.         return;
  262.     }
  263.  
  264.     for(y = baz->top; y < baz->bottom; y++) {
  265.         for(x = baz->left; x < baz->right; x++) {
  266.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  267.             quux = ((unsigned char*)pixelptr)[1];
  268.             quux += ((unsigned char*)pixelptr)[3];
  269.             quux >>= 1;
  270.             ((unsigned char*)pixelptr)[1] = ((unsigned char*)pixelptr)[3] = quux;
  271.         }
  272.     }
  273. }
  274.  
  275. void Flip(Rect *baz) {
  276.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  277.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  278.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  279.     long x, y;
  280.     unsigned long *top, *bottom;
  281.     unsigned long temp;
  282.  
  283.     if(!data) {
  284.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  285.         return;
  286.     }
  287.  
  288.     for(y = baz->top; y < baz->bottom - ((baz->bottom - baz->top + 1)>>1); y++) {
  289.         for(x = baz->left; x < baz->right; x++) {
  290.             top = (unsigned long*)(data + (y * rbytes) + (x * 4));
  291.             bottom = (unsigned long*)(data + ((baz->bottom - y - 1) * rbytes) + (x * 4));
  292.             temp = *bottom;
  293.             *bottom = *top;
  294.             *top = temp;
  295.         }
  296.     }
  297. }
  298.  
  299. void NoBlue(Rect *baz) {
  300.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  301.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  302.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  303.     long x, y;
  304.     unsigned long *pixelptr;
  305.     short temp;
  306.  
  307.     if(!data) {
  308.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  309.         return;
  310.     }
  311.  
  312.     for(y = baz->top; y < baz->bottom; y++) {
  313.         for(x = baz->left; x < baz->right; x++) {
  314.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  315.             temp = ((unsigned char*)pixelptr)[1] + ((unsigned char*)pixelptr)[2];
  316.             ((unsigned char*)pixelptr)[3] = temp>>1;
  317.         }
  318.     }
  319. }
  320.  
  321. void Trace(Rect *baz) {
  322.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  323.     unsigned long rbytes = (bar[0]->rowBytes & 0x3FFF)>>2;
  324.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  325.     long x, y;
  326.     unsigned long *from, *to;
  327.     unsigned long comp;
  328.     unsigned long *buffer = (unsigned long *)NewPtr( (baz->right - baz->left) * (baz->bottom - baz->top) * 4 );
  329.     Ptr testptr;
  330.  
  331.     testptr = NewPtr(1<<23);
  332.     if(!testptr) {
  333.         DebugStr("\pi couldn't allocate the bigass pointer. you lose");
  334.     } else {
  335.         DisposePtr(testptr);
  336.     }
  337.     
  338.     if(!data) {
  339.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  340.         return;
  341.     }
  342.  
  343.     if(!buffer) {
  344.         DebugStr("\pthe buffer is nil");
  345.         return;
  346.     }
  347.  
  348.     for(y = baz->top; y < baz->bottom; y++) {
  349.         for(x = baz->left; x < baz->right; x++) {
  350.             from = (unsigned long*)(data + (y * (rbytes<<2)) + (x * 4));
  351.             to = buffer + y*(baz->right - baz->left) + x;
  352.             
  353.             comp =
  354.                 ( ( *(from - 1) & 0xFCFCFCFC) >> 2) +
  355.                 ( ( *(from + 1) & 0xFCFCFCFC) >> 2) +
  356.                 ( ( *(from - rbytes) & 0xFCFCFCFC) >> 2) +
  357.                 ( ( *(from + rbytes) & 0xFCFCFCFC) >> 2);
  358.  
  359.             if( ((char*)from)[1] > ((char*)&comp)[1] )
  360.                 ((char*)to)[1] = ((char*)from)[1] - ((char*)&comp)[1];
  361.             else
  362.                 ((char*)to)[1] = ((char*)&comp)[1] - ((char*)from)[1];
  363.  
  364.             if( ((char*)from)[2] > ((char*)&comp)[2] )
  365.                 ((char*)to)[2] = ((char*)from)[2] - ((char*)&comp)[2];
  366.             else
  367.                 ((char*)to)[2] = ((char*)&comp)[2] - ((char*)from)[2];
  368.  
  369.             if( ((char*)from)[3] > ((char*)&comp)[3] )
  370.                 ((char*)to)[3] = ((char*)from)[3] - ((char*)&comp)[3];
  371.             else
  372.                 ((char*)to)[3] = ((char*)&comp)[3] - ((char*)from)[3];
  373.         }
  374.     }
  375.     for(y = baz->top; y < baz->bottom; y++) {
  376.         for(x = baz->left; x < baz->right; x++) {
  377.             *(unsigned long*)(data + (y * (rbytes<<2)) + (x * 4)) = buffer[y*(baz->right - baz->left) + x];
  378.         }
  379.     }
  380.     DisposePtr((char*)buffer);
  381. }
  382.  
  383. /*
  384.  * DebugStr() + printf() = very useful function
  385.  */
  386. void dprintf(char *format, ...) {
  387.     va_list args;
  388.     char dbugstr[256];
  389.  
  390.     va_start(args, format);
  391.     vsprintf(dbugstr, format, args);
  392.     va_end(args);
  393.  
  394.     DebugStr(CtoPstr((char*)dbugstr));
  395. }
  396.  
  397. void Right(Rect *baz) {
  398.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  399.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  400.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  401.     long x, y;
  402.     unsigned long *pixelptr;
  403.     char foo;
  404.  
  405.     if(!data) {
  406.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  407.         return;
  408.     }
  409.  
  410.     for(y = baz->top; y < baz->bottom; y++) {
  411.         for(x = baz->left; x < baz->right; x++) {
  412.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  413.             foo = ((char*)pixelptr)[1];
  414.             ((char*)pixelptr)[1] = ((char*)pixelptr)[2];
  415.             ((char*)pixelptr)[2] = ((char*)pixelptr)[3];
  416.             ((char*)pixelptr)[3] = foo;
  417.         }
  418.     }
  419. }
  420.  
  421. void Left(Rect *baz) {
  422.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  423.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  424.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  425.     long x, y;
  426.     unsigned long *pixelptr;
  427.     char foo;
  428.  
  429.     if(!data) {
  430.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  431.         return;
  432.     }
  433.  
  434.     for(y = baz->top; y < baz->bottom; y++) {
  435.         for(x = baz->left; x < baz->right; x++) {
  436.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  437.             foo = ((char*)pixelptr)[3];
  438.             ((char*)pixelptr)[3] = ((char*)pixelptr)[2];
  439.             ((char*)pixelptr)[2] = ((char*)pixelptr)[1];
  440.             ((char*)pixelptr)[1] = foo;
  441.         }
  442.     }
  443. }
  444.